package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.MealCalendarDao;
import com.neusoft.SPNursingHome.po.MealCalendar;
import com.neusoft.SPNursingHome.po.MealCalendarWithFood;
import com.neusoft.SPNursingHome.po.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MealCalendarServiceImpl implements MealCalendarService {
    @Autowired
    private MealCalendarDao dao;

    @Override
    public ResponseBean<List<MealCalendar>> listAll() {
        List<MealCalendar> list = dao.selectList(new QueryWrapper<>());
        return new ResponseBean<>(list);
    }

    @Override
    public ResponseBean<IPage<MealCalendarWithFood>> listWithFoodName(
            String mealCalendarDate,
            String mealCalendarTime,
            String foodName,
            String foodType,
            long page,
            long size) {

        // 1. 构造分页对象
        Page<MealCalendarWithFood> pager = new Page<>(page, size);

        // 2. 调用 DAO 中支持分页的方法
        IPage<MealCalendarWithFood> pageData =
                dao.selectWithFoodName(pager, mealCalendarDate, mealCalendarTime, foodName, foodType);

        return new ResponseBean<>(pageData);
    }

    @Override
    public ResponseBean<MealCalendar> getById(Integer id) {
        MealCalendar mc = dao.selectById(id);
        if (mc == null) {
            return new ResponseBean<>(404, "未找到对应记录");
        }
        return new ResponseBean<>(mc);
    }

    @Override
    public ResponseBean<Void> create(MealCalendar mealCalendar) {
        mealCalendar.setMealCalendarStatus("启用");
        int rows = dao.insert(mealCalendar);
        if (rows != 1) {
            return new ResponseBean<>(500, "新增失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<Void> update(MealCalendar mealCalendar) {
        int rows = dao.updateById(mealCalendar);
        if (rows != 1) {
            return new ResponseBean<>(500, "更新失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<Void> deleteById(Integer id) {
        int rows = dao.deleteById(id);
        if (rows != 1) {
            return new ResponseBean<>(500, "删除失败");
        }
        return new ResponseBean<>();
    }
}
